<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评定表名" prop="assessName">
        <el-input
          v-model="queryParams.assessName"
          placeholder="请输入评定表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading"  :data="dataList"   @selection-change="handleSelectionChange" >

      <el-table-column label="序号" type="index" width="50" align="center"/>

      <el-table-column label="患者姓名" align="center" prop="patientName">
        <template slot-scope="scope">
          <router-link :to="'/patient/detail/' + scope.row.patientId" class="link-type">
            <span>{{ scope.row.patientName }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="主治医生" align="center" prop="masterName" />

      <el-table-column label="评估医生" align="center" prop="doctorName" />

      <el-table-column label="评定表名" align="center" prop="assessName" />

      <el-table-column label="审核状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.status"/>
        </template>

      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleAudit(scope.row)"
            v-show="scope.row.status>1"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>

import {delTask, listAuditByDept, listAuditTask} from "@/api/gaoxin/taskdata";
import {getUserProfile} from "@/api/system/user";

export default {
  name: "Data",
  dicts: ['audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // task_data表格数据
      dataList: [],
      patientName: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        masterName: null,
        doctorName: null,
        assessName: null,
        patientId: null,
        masterId: null,
        doctorId: null,
        assessId: null,
        phase: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询task_data列表 */
    getList() {
      getUserProfile().then(res => {
        let role = res.roleGroup;
        let depts = res.deptGroup;

        let index1 = role.indexOf('管理员');
        let index2 = role.indexOf('高级角色');
        let index3 = role.indexOf('普通角色');
        if (index1 >= '0') {
          this.listAuditTaskList();
        } else if (index2 >= '0') {
          for (let i = 0; i < depts.length; i++) {
            if (depts[i].senior == '1') {
              let deptId = depts[i].deptId;
              this.listAuditTaskByDept(deptId);
              break;
            }
          }
        } else if (index3 >= '0') {
          let userId = res.data.userId;
          this.listAuditTaskByUserId(userId);
        }
      });

    },
    listAuditTaskList() {
      this.loading = true;
      this.queryParams.status = '2'; //未审核
      listAuditTask(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    listAuditTaskByUserId(userId) {
      this.loading = true;
      this.queryParams.doctorId = userId;
      this.queryParams.status = '2'; //未审核
      listAuditTask(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    listAuditTaskByDept(deptId) {
      this.loading = true;
      listAuditByDept(deptId).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      let taskId = row.taskId;
      this.$router.push('/workflow/audit/detail/' + taskId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids;
      this.$modal.confirm('是否确认删除task_data编号为"' + taskIds + '"的数据项？').then(function() {
        return delTask(taskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
